<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
        <el-form-item label="日志时间" prop="logTime" class="el-form-search-item">
          <el-date-picker clearable size="mini"
                          v-model="queryParams.logTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日志时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日志级别" prop="logLevel" class="el-form-search-item">
          <el-input
            v-model="queryParams.logLevel"
            placeholder="请输入日志级别"
            clearable
            size="mini"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="el-form-search-item">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table ref="list" :height="tableHeight" v-loading="loading" :data="entryList">
      <el-table-column label="日志时间" align="center" prop="logTime" width="180" />
      <el-table-column label="日志级别" align="center" prop="logLevel" />
      <el-table-column label="日志消息" align="center" prop="message" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日志信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日志文件id" prop="logFileId">
          <el-input v-model="form.logFileId" placeholder="请输入日志文件id" />
        </el-form-item>
        <el-form-item label="日志时间" prop="logTime">
          <el-date-picker clearable size="mini"
                          v-model="form.logTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日志时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日志级别" prop="logLevel">
          <el-input v-model="form.logLevel" placeholder="请输入日志级别" />
        </el-form-item>
        <el-form-item label="日志消息" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {detailDeyuLogFile} from "@/api/deyu/deyuLogFile";

export default {
  name: "DeyuLogEntry",
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日志信息表格数据
      entryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "id",
        isAsc: "desc",
        logFileId: null,
        logTime: null,
        logLevel: null,
        message: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        logFileId: [
          { required: true, message: "日志文件id不能为空", trigger: "blur" }
        ],
        logTime: [
          { required: true, message: "日志时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = JSON.parse(this.$route.query.data);
    this.getList();
  },
  methods: {
    /** 查询日志信息列表 */
    getList() {
      this.loading = true;
      detailDeyuLogFile(this.queryParams).then(response => {
        this.entryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        logFileId: null,
        logTime: null,
        logLevel: null,
        message: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 单击行选中数据
    handleRowClick(row, column, event) {
      this.$refs.list.toggleRowSelection(row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加日志信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEntry(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改日志信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEntry(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEntry(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delEntry(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
